package org.yxing.palpalgps.activity;

import org.yxing.palpalgps.BaseActivity;
import org.yxing.palpalgps.PalPalGPSApplication;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.ActionBar.OnActionBarListener;
import org.yxing.palpalgps.adapter.RecentChatsCursorAdapter;
import org.yxing.palpalgps.domain.UserInfo;
import org.yxing.palpalgps.models.RecentChatModel;
import org.yxing.palpalgps.provider.RecentChatsProvider;
import org.yxing.palpalgps.provider.RecentChatsProvider.RecentChats;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

/**
 * 最近聊天用户
 * @author yuxing
 *
 */
public class RecentChatsActivity extends BaseActivity implements OnItemClickListener, OnItemLongClickListener, OnActionBarListener{
    private static final String TAG = RecentChatsActivity.class.getSimpleName();
    ///private RecentChatsAdapter mAdapter;
    private RecentChatsCursorAdapter mAdapter;
    private ListView mListView;
    private UserInfo user;
    private LoadDataTask mTask;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recent_chats);
        
        ///mAdapter = new RecentChatsAdapter(this);
        mAdapter = new RecentChatsCursorAdapter(this);
        mListView = (ListView) findViewById(android.R.id.list);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);
        
        user = ((PalPalGPSApplication) getApplication()).getUser();
        
        mTask = new LoadDataTask();
        mTask.execute();
    }
    
    public void onResume() {
        super.onResume();
        ((HomeActivity) getParent()).getActionBar().setOnActionBarListener(this);
    }
    
    
    public void onDestroy() {
        super.onDestroy();
        
        if(!mTask.isCancelled()){
            mTask.cancel(true);
        }
        mTask = null;
        
        mAdapter.changeCursor(null);
    }
    
    class LoadDataTask extends AsyncTask<Void, Void, Cursor>{

        @Override
        protected Cursor doInBackground(Void... arg0) {
            String selection = RecentChats.UID + "=?";
            String[] selectionArgs = {user.getId()};
            Cursor cursor = getContentResolver().query(RecentChatsProvider.CONTENT_URI, null, selection, selectionArgs, null);
            
            return cursor;
        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            if(cursor == null || cursor.isClosed()){
                return;
            }
            mAdapter.changeCursor(cursor);
            
        }
    }
    
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        
        //go to chat activity
        RecentChatModel model = (RecentChatModel)view.getTag();
        
        ///RecentChatModel model = mAdapter.getItem(position);
        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra("fuid", model.getFuid());
        intent.putExtra("name", model.getFname());
        intent.putExtra("portrait", model.getPortrait());
        startActivity(intent);
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        final RecentChatModel model = (RecentChatModel)view.getTag();
        String[] items = new String[]{getString(R.string.delete_from_list)};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(model.getFname());
        builder.setItems(items, new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String where = RecentChats.UID + "=? AND " + RecentChats.FUID + "=?";
                String[] whereArgs = {user.getId(), model.getFuid()};
                getContentResolver().delete(RecentChatsProvider.CONTENT_URI, where, whereArgs);
            }
        });
        builder.create();
        builder.show();
        return false;
    }


    public void onActionBarItemClick(int position) {
        if (position == 0) {
            Intent intent = new Intent(this, GroupChatsActivity.class);
            startActivity(intent);
        }
    }
    
}
